package com.yxhl.km.module.person.mvp;

import com.yxhl.km.http.retrofit.IEssenceService;
import com.yxhl.km.module.login.entity.UserInfoBean;
import com.yxhl.km.module.person.entity.UploadBean;
import com.yxhl.km.module.person.entity.UserInfo;
import com.yxhl.km.module.utils.ToastUtil;
import com.yxhl.km.mvp.BaseModel;

import java.io.File;
import java.util.List;

import okhttp3.MultipartBody;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 张亚楠 on 2018/5/21.
 */

public class PersonMoudle extends BaseModel {
    /**
     * 获取个人信息
     *
     * @param userId
     * @param onResultobj
     */

    public void getData(String userId, final ResultBean<UserInfo> onResultobj) {
        buildService(IEssenceService.class)
                .getUserInfo(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<UserInfo>() {
                    @Override
                    public void call(UserInfo homeBean) {
                        onResultobj.onResult(homeBean);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwableHandler(throwable);
                    }
                });
    }

    /**
     * 上传头像
     *
     * @param parts
     * @param onResultobj
     */
    public void getUploadHeadData(List<MultipartBody.Part> parts, final ResultBean<UploadBean> onResultobj) {
        buildService(IEssenceService.class)
                .getUploadHead(parts)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<UploadBean>() {
                    @Override
                    public void call(UploadBean homeBean) {
                        onResultobj.onResult(homeBean);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwableHandler(throwable);
                    }
                });
    }

    public void throwableHandler(Throwable e) {
        ToastUtil.makeToast(e.getMessage());
    }


}
